<template>
	<view class="favorite-page">
		<!-- 分类标签 -->
		<view class="tabs-container">
			<u-tabs :list="tabs" :current="currentTab" @change="tabChange" :activeStyle="{color: '#ff2d51'}"
				:inactiveStyle="{color: '#666'}" lineColor="#ff2d51">
			</u-tabs>
		</view>
		<!-- 商品列表 -->
		<scroll-view scroll-y class="goods-list" :style="{height: scrollHeight + 'px'}" @scrolltolower="loadMore">
			<!-- 商品卡片 -->
			<view v-for="(item, index) in List" :key="index" class="goods-card">
				<view class="card-left">
					<u-image :src="item.goodsImage" width="180rpx" height="180rpx" radius="8"
						@click="goGoodsDetail(item.id)"></u-image>
					<view class="collect-icon" v-if="item.status==2">
						已失效
					</view>
				</view>

				<view class="card-right">
					<view class="goods-title" @click="goGoodsDetail(item.id)">
						{{item.goodsName}}
					</view>

					<view class="goods-spec" v-if="item.spec">
						规格：{{item.spec}}
					</view>

					<view class="price-section">
						<text class="current-price">¥{{item.currentPrice}}</text>
						<text class="original-price" v-if="item.originalPrice">¥{{item.originalPrice}}</text>
					</view>

					<view class="action-section">
						<u-button type="error" size="mini" shape="circle" text="取消收藏"
							@click="showmodelDialog(item.id)"></u-button>
					</view>
				</view>
			</view>
			<!-- 加载状态 -->
			<view class="load-status" @click="loadMore" v-if="this.List.length>0">
				<u-loadmore :status="loadStatus" :icon-type="loadStatus === 'loading' ? 'circle' : 'none'" />
			</view>
			<!-- 空状态 -->
			<u-empty v-if="List.length === 0 && !loading" mode="data"
				icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无收藏商品"></u-empty>
			<u-modal :show="showmodel" :show-cancel-button="true" content="确定要取消收藏吗？" @confirm="removemycollect"
					@cancel="showmodel=false"></u-modal>
		</scroll-view>
	</view>
</template>

<script>
	import {
		httpsend
	} from '@/api/api';
	export default {
		data() {
			return {
				currentTab: 0,
				tabs: [{
						name: '全部',
						badge: {
							value: 0,
						}
					},
					{
						name: '降价商品',
						badge: {
							value: 0,
						}
					},
					{
						name: '失效商品',
						badge: {
							value: 0,
						}
					}
				],
				scrollHeight: 0,
				List: [],
				showmodel: false,
				loading: false,
				loadStatus: 'loadmore',
				hasMore: true,
				page: 1,
				pageSize: 10,
				selectid: null,
			}
		},
		onLoad() {
			this.calcScrollHeight();
			this.getFavoriteList();
		},
		methods: {
			// 计算滚动区域高度
			calcScrollHeight() {
				const systemInfo = uni.getSystemInfoSync()
				this.scrollHeight = systemInfo.windowHeight
			},

			// 获取收藏列表
			async getFavoriteList() {
				if (this.loading || !this.hasMore) return;
				try {
					this.loading = true;
					this.loadStatus = 'loading';
					this.loading = true;
					let param={
						usrid:uni.getStorageSync('usrid'),
						type:this.currentTab,
						page:this.page,
						pageSize:this.pageSize
					}
					uni.showLoading({
						title: '加载中...'
					});
					const res = await httpsend('getmycollect',JSON.stringify(param));
					if (res?.code === 1) {
						//商品订单信息
						if (this.page == 1) {
							this.List = res.data;
						} else {
							this.List = [...this.List, ...res.data];
						}
						this.allcount = res.allcount;
						this.haddiscount = res.haddiscount;
						this.expire = res.expire;
						this.tabs[0]['badge']['value']=res.allcount;
						this.tabs[1]['badge']['value']=res.haddiscount;
						this.tabs[2]['badge']['value']=res.expire;
					if(this.currentTab!==0){
						this.List=this.List.filter(item=>item.status==this.currentTab);
					}		 					
					} else {
						this.$api.msg(res.msg);
					}
					uni.hideLoading();
					this.page++;
					this.loading = false;
					// 模拟分页结束
					if (this.page >= 3) {
						this.hasMore = false;
						this.loadStatus = 'nomore';
					} else {
						this.loadStatus = 'loadmore';
					}

				} catch (err) {
					uni.hideLoading();
					this.loading = false;
					console.log('err', err);
				}
			},

			// 切换标签
			tabChange(item) {
				this.currentTab = item.index;
				this.page = 1;
				this.hasMore = true;
				this.List = [];
				this.getFavoriteList();
			},
			// 加载更多
			loadMore() {
				if (this.loadStatus === 'nomore') return;
				this.getFavoriteList();
			},
			// 取消收藏
			async removemycollect() {
				try {
					uni.showLoading({
						title: '正在处理...'
					});
					const res = await httpsend('removemycollect');
					if (res?.code === 1) {
						this.List.splice(
							this.List.findIndex(item => item.id === this.selectid),
							1
						);
						this.showmodel=false;
					} else {
					}
					uni.hideLoading();
					this.$api.msg(res.msg);
				} catch (err) {
					uni.hideLoading();
					console.log('err', err);
				}
			},
			// 显示弹窗
			showmodelDialog(id) {
				this.selectid = id;
				this.showmodel = true;
			},
			// 跳转商品详情
			goGoodsDetail(id) {
				uni.navigateTo({
					url: `/pages/goodsinfo/goodsdetail?id=${id}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.favorite-page {
		height: 100vh;
		background-color: #f7f7f7;
	}

	.tabs-container {
		background-color: #fff;
		position: sticky;
		top: 0;
		z-index: 10;
	}

	.goods-list {
		padding: 20rpx;
		box-sizing: border-box;
	}

	.goods-card {
		display: flex;
		background-color: #fff;
		border-radius: 16rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		position: relative;

		.card-left {
			position: relative;
			margin-right: 20rpx;

			.collect-icon {
				position: absolute;
				top: 0;
				right: 0;
				width: 100rpx;
				height: 100rpx;
				background-color: rgba(255, 255, 255, 0.8);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.card-right {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-title {
				font-size: 28rpx;
				color: #333;
				line-height: 1.4;
				/* 替换u-line-clamp mixin */
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
			}

			.goods-spec {
				font-size: 24rpx;
				color: #999;
				margin: 10rpx 0;
			}

			.price-section {
				margin: 10rpx 0;

				.current-price {
					font-size: 32rpx;
					color: #ff2d51;
					font-weight: bold;
				}

				.original-price {
					font-size: 24rpx;
					color: #999;
					text-decoration: line-through;
					margin-left: 10rpx;
				}
			}

			.action-section {
				display: flex;
				justify-content: flex-end;
				margin-top: 10rpx;
				gap: 10rpx;
			}
		}
	}

	.load-status {
		padding: 20rpx 0;
	}

	/* 添加uView主题变量（如果需要） */
	:deep(.u-tabs__wrapper__nav__item) {
		flex: 1;
		justify-content: center;
	}
</style>